{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>dteday</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2011-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>331</td>\n",
       "      <td>654</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2011-01-02</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>131</td>\n",
       "      <td>670</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2011-01-03</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>120</td>\n",
       "      <td>1229</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2011-01-04</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>108</td>\n",
       "      <td>1454</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2011-01-05</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>82</td>\n",
       "      <td>1518</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant      dteday  season  yr  mnth  holiday  weekday  workingday  \\\n",
       "0        1  2011-01-01       1   0     1        0        6           0   \n",
       "1        2  2011-01-02       1   0     1        0        0           0   \n",
       "2        3  2011-01-03       1   0     1        0        1           1   \n",
       "3        4  2011-01-04       1   0     1        0        2           1   \n",
       "4        5  2011-01-05       1   0     1        0        3           1   \n",
       "\n",
       "   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n",
       "0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n",
       "1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n",
       "2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n",
       "3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n",
       "4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n",
       "\n",
       "    cnt  \n",
       "0   985  \n",
       "1   801  \n",
       "2  1349  \n",
       "3  1562  \n",
       "4  1600  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np  \n",
    "import pandas as pd \n",
    "\n",
    "from sklearn.metrics import r2_score  \n",
    "\n",
    "import matplotlib.pyplot as plt  \n",
    "import seaborn as sns\n",
    "\n",
    "\n",
    "data = pd.read_csv(\"day.csv\")\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(731, 16)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(584, 15)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = data['cnt'].values\n",
    "X = data.drop('cnt', axis = 1)\n",
    "X = data.drop('dteday', axis = 1)\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test,Y_train, Y_test = train_test_split(X,Y, random_state=33, test_size=0.2)\n",
    "\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zjkf\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n",
      "  warnings.warn(msg, DataConversionWarning)\n",
      "C:\\Users\\zjkf\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n",
      "  warnings.warn(msg, DataConversionWarning)\n",
      "C:\\Users\\zjkf\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n",
      "  warnings.warn(msg, DataConversionWarning)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "ss_X = StandardScaler()\n",
    "ss_y = StandardScaler()\n",
    "\n",
    "X_train = ss_X.fit_transform(X_train)\n",
    "X_test = ss_X.transform(X_test)\n",
    "\n",
    "Y_train = ss_y.fit_transform(Y_train.reshape(-1, 1))\n",
    "Y_test = ss_y.transform(Y_test.reshape(-1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>registered</td>\n",
       "      <td>[0.4549649631335762]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>cnt</td>\n",
       "      <td>[0.4372202762299621]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>casual</td>\n",
       "      <td>[0.19765016722538176]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[2.0754478727096636e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>[1.5677698917300788e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>[2.1707644021217693e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>[1.6161177024543928e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[-7.377032533818323e-18]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-9.622713639372836e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>[-1.7576250686672092e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>[-2.8175814675621353e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[-3.261642745104462e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[-5.497547520859137e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>[-5.521446417797028e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>[-5.848924828250829e-16]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       columns                       coef\n",
       "13  registered       [0.4549649631335762]\n",
       "14         cnt       [0.4372202762299621]\n",
       "12      casual      [0.19765016722538176]\n",
       "4      holiday   [2.0754478727096636e-16]\n",
       "7   weathersit   [1.5677698917300788e-16]\n",
       "10         hum   [2.1707644021217693e-17]\n",
       "5      weekday   [1.6161177024543928e-17]\n",
       "6   workingday   [-7.377032533818323e-18]\n",
       "11   windspeed   [-9.622713639372836e-17]\n",
       "8         temp  [-1.7576250686672092e-16]\n",
       "3         mnth  [-2.8175814675621353e-16]\n",
       "0      instant   [-3.261642745104462e-16]\n",
       "9        atemp   [-5.497547520859137e-16]\n",
       "1       season   [-5.521446417797028e-16]\n",
       "2           yr   [-5.848924828250829e-16]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#线性模型\n",
    "\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "lr = LinearRegression()\n",
    "lr.fit(X_train, Y_train)\n",
    "\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "columns = X.columns\n",
    "\n",
    "fs = pd.DataFrame({\"columns\":list(columns), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 1.0\n",
      "The r2 score of LinearRegression on train is 1.0\n"
     ]
    }
   ],
   "source": [
    "#模型评价\n",
    "\n",
    "#测试集\n",
    "print ('The r2 score of LinearRegression on test is', r2_score(Y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print ('The r2 score of LinearRegression on train is', r2_score(Y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'[147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164\\n 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182\\n 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200\\n 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218\\n 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236\\n 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254\\n 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272\\n 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290\\n 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308\\n 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326\\n 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344\\n 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362\\n 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380\\n 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398\\n 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416\\n 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434\\n 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452\\n 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470\\n 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488\\n 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506\\n 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524\\n 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542\\n 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560\\n 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578\\n 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596\\n 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614\\n 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632\\n 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650\\n 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668\\n 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686\\n 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704\\n 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722\\n 723 724 725 726 727 728 729 730] not in index'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-40-1afcb8d18b8b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mkf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mKFold\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn_splits\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mtrain_index\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtest_index\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mkf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m     \u001b[0mX_train\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mtrain_index\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m     \u001b[0mX_test\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mtest_index\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[0mY_train\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mY\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mtrain_index\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   2680\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mSeries\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mIndex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2681\u001b[0m             \u001b[1;31m# either boolean or fancy integer index\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2682\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_array\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2683\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2684\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_frame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m_getitem_array\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   2724\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_take\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2725\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2726\u001b[1;33m             \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_convert_to_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2727\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_take\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2728\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_convert_to_indexer\u001b[1;34m(self, obj, axis, is_setter)\u001b[0m\n\u001b[0;32m   1325\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0mmask\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0many\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1326\u001b[0m                     raise KeyError('{mask} not in index'\n\u001b[1;32m-> 1327\u001b[1;33m                                    .format(mask=objarr[mask]))\n\u001b[0m\u001b[0;32m   1328\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1329\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_values_from_object\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: '[147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164\\n 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182\\n 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200\\n 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218\\n 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236\\n 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254\\n 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272\\n 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290\\n 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308\\n 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326\\n 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344\\n 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362\\n 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380\\n 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398\\n 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416\\n 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434\\n 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452\\n 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470\\n 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488\\n 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506\\n 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524\\n 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542\\n 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560\\n 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578\\n 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596\\n 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614\\n 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632\\n 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650\\n 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668\\n 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686\\n 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704\\n 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722\\n 723 724 725 726 727 728 729 730] not in index'"
     ]
    }
   ],
   "source": [
    "\n",
    "from sklearn.model_selection import KFold\n",
    "\n",
    "kf = KFold(n_splits=5)\n",
    "for train_index, test_index in kf.split(X):\n",
    "    X_train = X[train_index]\n",
    "    X_test = X[test_index]\n",
    "    Y_train = Y[train_index]\n",
    "    Y_test =  Y[test_index]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>registered</td>\n",
       "      <td>[0.4549649631335762]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>cnt</td>\n",
       "      <td>[0.4372202762299621]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>casual</td>\n",
       "      <td>[0.19765016722538176]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[2.0754478727096636e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>[1.5677698917300788e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>[2.1707644021217693e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>[1.6161177024543928e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[-7.377032533818323e-18]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-9.622713639372836e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>[-1.7576250686672092e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>[-2.8175814675621353e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[-3.261642745104462e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[-5.497547520859137e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>[-5.521446417797028e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>[-5.848924828250829e-16]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       columns                       coef\n",
       "13  registered       [0.4549649631335762]\n",
       "14         cnt       [0.4372202762299621]\n",
       "12      casual      [0.19765016722538176]\n",
       "4      holiday   [2.0754478727096636e-16]\n",
       "7   weathersit   [1.5677698917300788e-16]\n",
       "10         hum   [2.1707644021217693e-17]\n",
       "5      weekday   [1.6161177024543928e-17]\n",
       "6   workingday   [-7.377032533818323e-18]\n",
       "11   windspeed   [-9.622713639372836e-17]\n",
       "8         temp  [-1.7576250686672092e-16]\n",
       "3         mnth  [-2.8175814675621353e-16]\n",
       "0      instant   [-3.261642745104462e-16]\n",
       "9        atemp   [-5.497547520859137e-16]\n",
       "1       season   [-5.521446417797028e-16]\n",
       "2           yr   [-5.848924828250829e-16]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#岭回归\n",
    "from sklearn.linear_model import RidgeCV\n",
    "alphas = [0.01, 0.1, 1, 10,20, 30, 50, 60, 80,100]\n",
    "reg = RidgeCV(alphas=alphas, store_cv_values=True)\n",
    "reg.fit(X_train, Y_train)\n",
    "\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "fs = pd.DataFrame({\"columns\":list(columns), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 1.0\n",
      "The r2 score of LinearRegression on train is 1.0\n"
     ]
    }
   ],
   "source": [
    "#模型评价\n",
    "\n",
    "#测试集\n",
    "print ('The r2 score of LinearRegression on test is', r2_score(Y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print ('The r2 score of LinearRegression on train is', r2_score(Y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zjkf\\Anaconda3\\lib\\site-packages\\sklearn\\linear_model\\coordinate_descent.py:1094: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n",
      "C:\\Users\\zjkf\\Anaconda3\\lib\\site-packages\\sklearn\\linear_model\\coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>registered</td>\n",
       "      <td>[0.4549649631335762]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>cnt</td>\n",
       "      <td>[0.4372202762299621]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>casual</td>\n",
       "      <td>[0.19765016722538176]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[2.0754478727096636e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>[1.5677698917300788e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>[2.1707644021217693e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>[1.6161177024543928e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[-7.377032533818323e-18]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-9.622713639372836e-17]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>[-1.7576250686672092e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>[-2.8175814675621353e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[-3.261642745104462e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[-5.497547520859137e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>[-5.521446417797028e-16]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>[-5.848924828250829e-16]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       columns                       coef\n",
       "13  registered       [0.4549649631335762]\n",
       "14         cnt       [0.4372202762299621]\n",
       "12      casual      [0.19765016722538176]\n",
       "4      holiday   [2.0754478727096636e-16]\n",
       "7   weathersit   [1.5677698917300788e-16]\n",
       "10         hum   [2.1707644021217693e-17]\n",
       "5      weekday   [1.6161177024543928e-17]\n",
       "6   workingday   [-7.377032533818323e-18]\n",
       "11   windspeed   [-9.622713639372836e-17]\n",
       "8         temp  [-1.7576250686672092e-16]\n",
       "3         mnth  [-2.8175814675621353e-16]\n",
       "0      instant   [-3.261642745104462e-16]\n",
       "9        atemp   [-5.497547520859137e-16]\n",
       "1       season   [-5.521446417797028e-16]\n",
       "2           yr   [-5.848924828250829e-16]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Lasso 回归\n",
    "\n",
    "from sklearn.linear_model import LassoCV\n",
    "reg = LassoCV(cv=5, random_state=0)\n",
    "reg.fit(X_train, Y_train)\n",
    "\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "fs = pd.DataFrame({\"columns\":list(columns), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 1.0\n",
      "The r2 score of LinearRegression on train is 1.0\n"
     ]
    }
   ],
   "source": [
    "#模型评价\n",
    "\n",
    "#测试集\n",
    "print ('The r2 score of LinearRegression on test is', r2_score(Y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print ('The r2 score of LinearRegression on train is', r2_score(Y_train, y_train_pred_lr))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
